package dao

import (
	"context"
	"github.com/gin-gonic/gin"
	"gorm.io/gorm"
	"ylm.sl/internal/model"
)

type ShortLinkDao struct {
	db *gorm.DB
}

func (d *ShortLinkDao) Insert(c context.Context, entity *model.ShortLink) error {
	return d.db.WithContext(c).Model(&model.ShortLink{}).Create(entity).Error
}

func (d *ShortLinkDao) SelectOne(c *gin.Context, originUrl string, fullShortUrl string, enableStatus int) (*model.ShortLink, error) {
	entity := &model.ShortLink{}
	err := d.db.WithContext(c).Model(&model.ShortLink{}).Select("origin_url = ? and full_short_url = ? and enable_status = ?", originUrl, fullShortUrl, enableStatus).First(&entity).Error
	return entity, err
}

func (d *ShortLinkDao) Update(c *gin.Context, do *model.ShortLink) error {
	return d.db.WithContext(c).Model(&model.ShortLink{}).Updates(do).Error
}

func NewShortLinkDao(db *gorm.DB) *ShortLinkDao {
	return &ShortLinkDao{db: db}
}
